//
//  LAMapView.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2017/4/8.
//  Copyright © 2017年 2Bulu. All rights reserved.
//

#import "LAGMapView.h"

#define DEFAULT_CLUSTER_MAKE_RADIUM 30;


@implementation LAGMapView

#pragma mark- initial

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    [self publicInit];
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    [self publicInit];
    return self;
}

-(instancetype)init
{
    self = [super init];
    [self publicInit];
    return self;
}

-(void)publicInit
{
    _clusterRadium = DEFAULT_CLUSTER_MAKE_RADIUM;
    _offMapAnnotations = [NSMutableArray arrayWithCapacity:1000];
    _allClusterGroups = [NSMutableArray arrayWithCapacity:5];
    self.delegate = self;
}

#pragma mark- setter/getter

-(void)setDelegate:(id<MKMapViewDelegate>)delegate
{
    if(delegate == self)
        [super setDelegate:delegate];
    else
        _revisedDelegate = delegate;
    
}

-(id<MKMapViewDelegate>)delegate
{
    return _revisedDelegate;
}


#pragma mark- zoom

-(float)zoomLevel
{
    return [self zoomLevelForMapRect:self.visibleMapRect];
}

-(float)zoomLevelForMapRect:(MKMapRect)mapRect
{
    MKMapPoint orgMapPoint = mapRect.origin;
    MKMapPoint topLeftMapPoint = MKMapPointMake(orgMapPoint.x+mapRect.size.width, orgMapPoint.y);
    
    CLLocationCoordinate2D topRightCord = MKCoordinateForMapPoint(orgMapPoint);
    CLLocationCoordinate2D topLeftCord = MKCoordinateForMapPoint(topLeftMapPoint);
    
    CGPoint topRightPt = [self convertCoordinate:topRightCord toPointToView:self];
    CGPoint topLeftPt = [self convertCoordinate:topLeftCord toPointToView:self];
    
    CGFloat pointWidth = fabs(topLeftPt.x - topRightPt.x);
    
    float zoomLevel = log2(360 * ((pointWidth/256) / self.region.span.longitudeDelta));
    
    //  NSLog(@"self.camera.heading:%.2f zoomLevel:%.2f\npointWidth:%.1f longitudeDelta:%.7f  ratio:%.7f",self.camera.heading,zoomLevel,pointWidth,self.region.span.longitudeDelta,pointWidth/self.region.span.longitudeDelta);
    // NSLog(@"topRightPt(%.1f,%.1f)  topLeftPt(%.1f,%.1f)",topRightPt.x,topRightPt.y,topLeftPt.x,topLeftPt.y);
    
    return zoomLevel;
}


#pragma mark-
#if TARGET_OS_IPHONE
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
        UIView *v = [super hitTest:point withEvent:event];
#else
- (nullable NSView *)hitTest:(NSPoint)point
{
        NSView *v = [super hitTest:point];
#endif

    if (!self.igoreAnnotationViewTouchEvent) {
        return v;
    }
    
    //下面忽略响应 Annation 事件
    Class class1 = NSClassFromString(@"MKNewAnnotationContainerView");//ios11及之前版本
    Class class2 = NSClassFromString(@"MKAnnotationContainerView"); //ios12及之后版本
    
    if(![v isKindOfClass:class1]
       && ![v isKindOfClass:class2])
        return self;
    else
        return v;
}



#if DEBUG
-(void)dealloc
{
    NSLog(@"Dealloc %@ ",NSStringFromClass([self class]));
}
#endif


@end
